/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package siscom.componentes;

/**
 *
 * @author Dudu
 */
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.text.JTextComponent;

public class AutoCompleteComboBox extends JComboBox {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2844296353472685019L;
	private boolean enterChangeFocus = true;
	private FixedLengthDocument documento = new FixedLengthDocument(10, true);
	private boolean upperCase = true;
	private int maxLength = 0;

	public void setEnterChangeFocus(boolean enterChangeFocus) {
		this.enterChangeFocus = enterChangeFocus;
	}

	public boolean getEnterChangeFocus() {
		return this.enterChangeFocus;
	}

	public AutoCompleteComboBox() {
		super();
		initialize(false);
		addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER && enterChangeFocus) {
					transferFocus();
				}
			}

		});
	}

	public AutoCompleteComboBox(ComboBoxModel aModel) {
		super(aModel);
		initialize(false);
	}

	public AutoCompleteComboBox(Object... items) {
		super(items);
		initialize(false);
	}

	public AutoCompleteComboBox(Collection<?> items) {
		super(new Vector<Object>(items));
		initialize(false);
	}

	public AutoCompleteComboBox(boolean editable) {
		super();
		initialize(editable);
	}

	public AutoCompleteComboBox(ComboBoxModel aModel, boolean editable) {
		super(aModel);
		initialize(editable);
	}

	public AutoCompleteComboBox(Collection<?> items, boolean editable) {
		super(new Vector<Object>(items));
		initialize(editable);
	}

	private void initialize(boolean editable) {
		setEditable(editable);
		((JTextComponent) getEditor().getEditorComponent())
				.setDocument(documento);

		AutoCompleteDecorator.decorate(this);
		// final Color gray = getBackground();
		// final Color green =
		// UIManager.getColor("ComboBox.selectionBackground");
		// final Color selection = ((JTextComponent)
		// getEditor().getEditorComponent()).getSelectionColor();

		// getEditor().getEditorComponent().setBackground(Color.white);
		// getEditor().getEditorComponent().setForeground(Color.black);
		// ((JTextComponent)
		// getEditor().getEditorComponent()).setSelectionColor(Color.white);
		// ((JTextComponent)
		// getEditor().getEditorComponent()).setCaretColor(Color.white);
		// ((JTextComponent)
		// getEditor().getEditorComponent()).setForeground(((JTextComponent)
		// getEditor().getEditorComponent()).getSelectedTextColor());

		getEditor().getEditorComponent().addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				// getEditor().getEditorComponent().setBackground(green);
				// ((JTextComponent)
				// getEditor().getEditorComponent()).setSelectionColor(green);
			}

			public void focusLost(FocusEvent e) {
				// getEditor().getEditorComponent().setBackground(Color.white);
				// ((JTextComponent)
				// getEditor().getEditorComponent()).setSelectionColor(Color.white);
			}
		});

	}

	@Override
	public void setEnabled(boolean b) {
		super.setEnabled(b);
		getEditor().getEditorComponent().setBackground(getBackground());
	}

	public void setUpperCase(boolean upperCase) {
		this.upperCase = upperCase;
//		documento.setUpperCase(upperCase);
	}

	public boolean isUpperCase() {
		return upperCase;
	}

	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
//		documento.setMaxLength(maxLength);
	}

	public int getMaxLength() {
		return maxLength;
	}
}